package edu.pku.sei.transformation.editor.model.implement;

import java.util.List;

import org.eclipse.core.resources.IFile;
import org.eclipse.core.resources.IProject;
import org.eclipse.core.runtime.IPath;
import org.eclipse.core.runtime.Path;
import org.w3c.dom.Element;

import edu.pku.sei.modeler.model.core.MetaModelElement;
import edu.pku.sei.modeler.model.xml.XMLUtility;
import edu.pku.sei.modeler.project.AbstractModelingProject;
import edu.pku.sei.modeler.project.ProjectRegistry;
import edu.pku.sei.transformation.editor.model.ModelPackage;

public class TransformationCode extends MetaModelElement{
	protected String language = "";
	protected String filePath = "";
	protected IFile file = null;
	
	public IFile getFile() {
		if (file != null) {
			return file;
		} else {
			IProject proj = null;
			List projs = ProjectRegistry.getDefault().getProjects();
			for (int i = 0; i < projs.size(); i++) {
				AbstractModelingProject ap = (AbstractModelingProject) projs.get(i);
				if (ap.getModel() == getModel()) {
					proj = ap.getProject();
					break;
				}
			}
			if (proj == null) {
				return null;
			}
			IPath projPath = proj.getLocation();
			IPath path = new Path(filePath);
			//absolute path to relative
			if(projPath.isPrefixOf(path)) {
				path = new Path(path.toString().substring(projPath.toString().length()));
			}
			file = proj.getFile(path);
			if (!file.exists())
				file = null;
			return file;			
		}
		
	}
	
	public void setFile(IFile file) {
		this.file = file;
	}
	
	public String getLanguage() {
		return language;
	}
	public void setLanguage(String language) {
		this.language = language;
	}
	public String getLocalPath() {
		return filePath;
	}
	public void setLocalPath(String filePath) {
		IProject proj = null;
		List projs = ProjectRegistry.getDefault().getProjects();
		for (int i = 0; i < projs.size(); i++) {
			AbstractModelingProject ap = (AbstractModelingProject) projs.get(i);
			if (ap.getModel() == getModel()) {
				proj = ap.getProject();
				break;
			}
		}
		if (proj == null) {
			this.filePath = filePath;
		} else {
			IPath projPath = proj.getLocation();
			IPath path = new Path(filePath);
			//absolute path to relative
			if(projPath.isPrefixOf(path)) {
				path = new Path(path.toString().substring(projPath.toString().length()));
				this.filePath = path.toString();
			} else {
				this.filePath = filePath;
			}
			
		}
	}
	public String getAbsolutePath() {
		IFile file = getFile();
		if (file!=null && file.exists())
			return file.getLocation().toString();
		else
			return getLocalPath();
		
	}
	
	@Override
	public void deSerialize(Element root) {
		super.deSerialize(root);
		this.language = XMLUtility.xmlInstance.deSerializeString(root, ModelPackage.TRANSCODE_LANGUAGE);
		this.filePath = XMLUtility.xmlInstance.deSerializeString(root, ModelPackage.TRANSCODE_FILE);
	}
	
	@Override
	public void serialize(Element root) {
		super.serialize(root);
		XMLUtility.xmlInstance.serializeAttribute(root, ModelPackage.TRANSCODE_LANGUAGE, language);
		XMLUtility.xmlInstance.serializeAttribute(root, ModelPackage.TRANSCODE_FILE, filePath);
	}
}
